import { createStore, applyMiddleware, compose, combineReducers } from 'redux'
import thunk from 'redux-thunk'
import counterReducer from './counter'
import homeReducer from './home'

//将两个reducer合并在一起
const reducer = combineReducers({
  counter: counterReducer,
  home: homeReducer
})

/*
//combineReducers实现原理
function reducer(state = {}, action) {
  //返回一个对象，这个对象会作为sotre的state
  //只是缺少判断当前state是否发生改变,再决定是否执行独立的reducer函数
  return {
    counter: counterReducer(state.counter, action),
    home: homeReducer(state.home, action)
  }
}
*/

//redux-devtools
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({trace: true}) || compose;

//使用 redux-thunk 中间件
const store = createStore(reducer, composeEnhancers(applyMiddleware(thunk)));

export default store
